package com.zekdot.registerconsumer.action;

import com.alibaba.fastjson.JSONObject;
import com.zekdot.registerconsumer.entity.EnrollDataEntity;
import com.zekdot.registerconsumer.entity.EnrollMetaEntity;
import com.zekdot.registerconsumer.entity.EnrollTempletEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Create by zekdot on 20-4-4.
 */
@Controller
@RequestMapping("/enroll")
public class EnrollAction {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/newEnroll",method = RequestMethod.POST)
    @ResponseBody
    public String newEnroll(EnrollMetaEntity enrollMetaEntity, long planEndTimeLong, String userId){
//        System.out.println(planEndTimeLong);
        Map map = JSONObject.parseObject(JSONObject.toJSONString(enrollMetaEntity),Map.class);
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        for(Object key :map.keySet()){
            parammap.add((String)key,map.get(key));
        }
        parammap.add("planEndTimeLong",planEndTimeLong);
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/newEnroll",entity,String.class);
    }

    @RequestMapping(value = "/preEnrollUsers",method = RequestMethod.POST)
    @ResponseBody
    public String addPreEnrollUsers(String usernamesStr,String eId,String userId){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("usernamesStr",usernamesStr);
        parammap.add("eId",eId);
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/preEnrollUsers",entity,String.class);
    }

    @RequestMapping("/moreEnrollMeta")
    @ResponseBody
    public String moreEnrollMeta(String userId, long lastTimeLong,int length){
        String url = "http://register-client/enroll/moreEnrollMeta?userId=" + userId + "&lastTimeLong="
                + lastTimeLong + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 存储一个模板
     * @param userId 用户id
     * @param enrollTempletEntity 模板实体
     * @return
     */
    @RequestMapping(value = "/saveTemplet",method = RequestMethod.POST)
    @ResponseBody
    public String saveTemplet(String userId, EnrollTempletEntity enrollTempletEntity){

        Map map = JSONObject.parseObject(JSONObject.toJSONString(enrollTempletEntity),Map.class);
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        for(Object key :map.keySet()){
            parammap.add((String)key,map.get(key));
        }
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/saveTemplet",entity,String.class);
    }

    /**
     * 获取模板
     * @param id 模板id
     * @return
     */
    @RequestMapping("/getTemplet")
    @ResponseBody
    public String getTemplet(String userId,String id){
        String url = "http://register-client/enroll/getTemplet?userId=" + userId + "&id=" + id;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 根据关键字搜索模板列表
     * @param title 模板名称关键字
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @RequestMapping("/searchTemplets")
    @ResponseBody
    public String searchTemplets(String userId,String title,String lastId,int length){
        String url = "http://register-client/enroll/searchTemplets?userId=" + userId + "&title=" +
                title + "&lastId=" + lastId + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 根据关键字搜索模板列表
     * @param userId 模板名称关键字
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @RequestMapping("/selfTemplets")
    @ResponseBody
    public String selfTemplets(String userId,String lastId,int length){
        String url = "http://register-client/enroll/selfTemplets?userId=" + userId + "&lastId=" + lastId
                + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 删除模板
     * @param userId 用户id
     * @param id 模板id
     * @return
     */
    @RequestMapping(value = "/deleteTemplet",method = RequestMethod.POST)
    @ResponseBody
    public String deleteTemplet(String userId,String id){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("id",id);
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/deleteTemplet",entity,String.class);
    }

    /**
     * 获取报名元数据
     * @param eId 元数据id
     * @param token 令牌
     * @param userId 用户名
     * @return
     */
    @RequestMapping("/getEnrollMeta")
    @ResponseBody
    public String getEnrollMeta(String eId,String token,String userId){
        String url = "http://register-client/enroll/getEnrollMeta?userId=" + userId + "&token=" + token
                + "&eId=" + eId;
        return restTemplate.getForObject(url,String.class);
    }
    /**
     * 提交报名数据
     * @param userId
     * @param enrollDataEntity
     * @return
     */
    @RequestMapping(value = "/submitEnroll",method = RequestMethod.POST)
    @ResponseBody
    public String submitEnroll(String userId, EnrollDataEntity enrollDataEntity){
        Map map = JSONObject.parseObject(JSONObject.toJSONString(enrollDataEntity),Map.class);
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        for(Object key :map.keySet()){
            parammap.add((String)key,map.get(key));
        }
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/submitEnroll",entity,String.class);
    }

    /**
     * 获取报名结果
     * @param userId 用户id
     * @param eId 报名id
     * @return
     */
    @RequestMapping("/enrollResult")
    @ResponseBody
    public String enrollResult(String userId,String eId){
        String url = "http://register-client/enroll/enrollResult?userId=" + userId + "&eId=" + eId;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取自己填写的报名列表
     * @param userId
     * @return
     */
    @RequestMapping("/moreEnrollData")
    @ResponseBody
    public String moreEnrollData(String userId, long lastTimeLong,int length){
        String url = "http://register-client/enroll/moreEnrollData";
        url = url + "?userId=" + userId + "&lastTimeLong=" + lastTimeLong + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取报名元数据的描述
     * @param userId 用户名
     * @param eId 报名id
     * @param token 令牌
     * @return
     */
    @RequestMapping("/metaDesc")
    @ResponseBody
    public String metaDesc(String userId,String eId,String token){
        String url = "http://register-client/enroll/metaDesc";
        url = url + "?userId=" + userId + "&eId=" + eId + "&token=" + token;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取所有报名数据的描述部分
     * @param userId 用户id
     * @param eId 报名id
     * @param lastTimeLong 最后一条的时间getTime
     * @param length 长度
     * @return
     */
    @RequestMapping("/dataDescs")
    @ResponseBody
    public String dataDescs(String userId,String eId,long lastTimeLong,int length){
        String url = "http://register-client/enroll/dataDescs";
        url = url + "?userId=" + userId + "&eId=" + eId + "&lastTimeLong=" + lastTimeLong + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取报名数据的详情
     * @param userId
     * @param eId
     * @param username
     * @return
     */
    @RequestMapping("/enrollDataDetail")
    @ResponseBody
    public String enrollDataDetail(String userId,String eId,String username){
        String url = "http://register-client/enroll/enrollDataDetail";
        url = url + "?userId=" + userId + "&eId=" + eId + "&username=" + username;
        return restTemplate.getForObject(url,String.class);
    }

    @RequestMapping(value = "/updateDataStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateDataStatus(String userId,String eId,String username,int status,String rejectReason){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("status",status);
        parammap.add("eId",eId);
        parammap.add("username",username);
        parammap.add("rejectReason",rejectReason);
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/updateDataStatus",entity,String.class);
    }

    @RequestMapping(value = "/updateMetaStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateMetaStatus(String userId,String eId){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("eId",eId);
        parammap.add("userId",userId);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/updateMetaStatus",entity,String.class);
    }

    /**
     * 根据报名id和token获取当前未审核的用户列表
     * @param eId 报名id
     * @param operaToken 操作token
     * @param length 列表长度
     * @return
     */
    @RequestMapping(value = "/enrollDataByToken")
    @ResponseBody
    public String getEnrollDataByToken(String eId,String operaToken,int length){
        String url = "http://register-client/enroll/enrollDataByToken";
        url = url + "?eId=" + eId + "&operaToken=" + operaToken + "&length=" + length;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 审核报名列表中的用户
     * @param eId 报名id
     * @param reqArrayStr 审核JSON字符串
     * @param operaToken 操作token
     * @return
     */
    @RequestMapping(value = "/checkUserByToken",method = RequestMethod.POST)
    @ResponseBody
    public String checkUserByToken(String eId,String reqArrayStr,String operaToken){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("eId",eId);
        parammap.add("reqArrayStr",reqArrayStr);
        parammap.add("operaToken",operaToken);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://register-client/enroll/checkUserByToken",entity,String.class);
    }

    /**
     * 获取临时操作token
     * @param eId
     * @return
     */
    @RequestMapping("/getTempToken")
    @ResponseBody
    public String getTempToken(String eId){
        String url = "http://register-client/enroll/getTempToken";
        url = url + "?eId=" + eId;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取全部通过用户的报名数据
     * @param tempToken
     * @return
     */
    @RequestMapping("/getExcelData")
    @ResponseBody
    public String getExcelData(String tempToken){
        String url = "http://register-client/enroll/getExcelData";
        url = url + "?tempToken=" + tempToken;
        return restTemplate.getForObject(url,String.class);
    }

    /**
     * 获取重用报名数据
     * @param eId 报名id
     * @return
     */
    @RequestMapping("/getReuseEnrollData")
    @ResponseBody
    public String getReuseEnrollData(String eId){
        String url = "http://register-client/enroll/getReuseEnrollData";
        url = url + "?eId=" + eId;
        return restTemplate.getForObject(url,String.class);
    }

}
